<template>
	<div>
		<!-- 模态框 -->
		<el-dialog style="text-align: left;" title="添加产品档次级别" :visible.sync="dialogVisible" width="30%">
			<span>
				<el-row>
					<el-input placeholder="请输入产品档次名称" v-model="prolevelName" class="input-with-select">
						<span slot="prepend">档次名称：</span>
					</el-input>
				</el-row>
			</span>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="AddprolevelMessage()">确 定</el-button>
			</span>
		</el-dialog>
		<el-container>
			<el-header>
				<el-row>
					<el-row style="text-align: center; margin-top: 10px; margin-bottom: 20px;">
						<!-- 新增产品用途类型 -->
						<el-col :span="3" :offset="18">
							<el-button size="small" type="success" @click="dialogVisible=true">添加</el-button>
						</el-col>
						<!-- 批量删除按钮 -->
						<el-col :span="3">
							<el-button size="small" type="danger" @click="DeleteProlevelByNums()">删除</el-button>
						</el-col>
					</el-row>
				</el-row>
			</el-header>
			<el-main style="height: 465px; overflow: hidden; background: white; border:1px solid white ; border-radius: 8px; margin-top: 10px;">
				<div>
					<el-table :data="productLevel" border style="width: 100%" @selection-change="handleSelectionChang">
							<el-table-column header-align="center" align="center" type="selection"></el-table-column>
						<el-table-column fixed prop="levelnum" label="档次编号">
						</el-table-column>
						<el-table-column prop="levelName" label="档次名称">
						</el-table-column>
						<el-table-column fixed="right" label="操作" width="200px">
							<template slot-scope="scope">
								<el-button type="danger" size="small" @click.native.prevent="DeleteProlevelByNum(scope.row)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
				</div>
				<div>
					<template>
						<!--@size-change：当条数发现改变时触发-->
						<!--@current-change：当页码发生改变时触发-->
						<el-pagination style="margin-top: 5px;" layout="sizes, prev, pager, next, jumper, ->, total" :total="total"
						 :page-size.sync="pageSize" :page-sizes="pageSizes" :current-page.sync="currentPage" :pager-count="pagerCount"
						 background @size-change="handleSizeChange" @current-change="handleCurrentChange">
						</el-pagination>
					</template>
				</div>
			</el-main>
		</el-container>
	</div>
</template>

<script>
	export default {
		name: 'productGradeOfType',
		data() {
			return {
				/* 当前时间 */
				nowTime: '',
				/* 模态框显示 */
				dialogVisible: false,
				/* 分页 */
				total:1, //总条数
				pageSize: 5, //每页显示多少条
				pageSizes: [2, 3, 5], //每页显示条数候选值
				currentPage: 1, //当前页
				pagerCount: 5, //最多显示的页码，多余的会折叠
				productLevel: [],
				/* 添加产品档次需要用到的产品档次名称 */
				prolevelName:'',
				/* 传入数据库的iId数组 */
				nums:[],
				/* 用户选择的行的编号，用于批量删除 */
				selections:[],
			}
		},
		mounted: function() {
			/* 页面加载时启动计时器 */
			this.currentTime();
			/* 获取当前页所有的用途类型 */
			this.SelectProductLevelByNumName();
		},
		methods: {
			/*
			 * 可以根据条件查询一级分类也可以不根据查询一级分类
			 * this.pageSize			每页显示的条数
			 * this.currentPage			当前页码
			 * this.conditionProName	需要查询的分类名称
			 */
			SelectProductLevelByNumName(){
				var params = {
					"pageNum": this.currentPage,
					"pageSize": this.pageSize
				};
				
				this.$http.post("design/prolevel/getFindProlevelByPagerMessage",this.$qs.stringify(params)).then(res => {
					console.log(res.data);
					//设置当前页数据
					this.productLevel = res.data.rows;
					//总页码设置
					this.total = res.data.total;
				})
			},
			
			/**
			 * 添加产品档次信息
			 * this.prolevelName：产品档次名称
			 */
			AddprolevelMessage(){
				this.$http.post("design/prolevel/getInsertProlevelByNumName","levelName=" + this.prolevelName).then(res => {
					if (res.data > 0) {
						this.$message.success('添加产品档次成功！');
						/* 关闭模态框 */
						this.dialogVisible = false;
						/* 清空文本框 */
						this.prolevelName = '';
						/* 刷新数据 */
						this.SelectProductLevelByNumName();
					} else {
						this.$message.error('添加产品档次失败！');
						/* 关闭模态框 */
						this.dialogVisible = false;
						/* 清空文本框 */
						this.prolevelName = '';
						/* 刷新数据 */
						this.SelectProductLevelByNumName();
					}
				})
			},
			
			/**
			 * 删除产品档次信息
			 */
			DeleteProlevelByNums(){
				console.log(this.nums);
				
				/* 循环解析选中的行，取出id值 */
				if(this.selections.length > 0){
					for (var i = 0; i < this.selections.length; i++) {
						this.nums[i] = this.selections[i].levelnum;
					}
				}
				/* 数据库批量删除 */
				this.$http.post("design/prolevel/getDeleteProlevelByNums", "levelnums=" + this.nums).then(res => {
					this.SelectProductLevelByNumName();
					if (res.data > 0) {
						/* 刷新数据 */
						this.$message.success('删除产品档次信息成功！');
						if((this.total - this.iIds.length) % this.pageSize == 0 && this.currentPage != 1){
							this.currentPage = this.currentPage-1;
						}
					} else {
						/* 刷新数据 */
						this.$message.error('删除产品档次信息失败！');
					}
				})
			},
			
			/**
			 * 删除单个产品档次表
			 */
			DeleteProlevelByNum(row){
				this.nums[0] = row.levelnum;
				/* 调用批量删除 */
				this.DeleteProlevelByNums();
			},
			
			/* 改变size[每页条数]调用方法*/
			handleSizeChange(size) {
				/* 只要改变马上调用分页查询 */
				this.SelectProductLevelByNumName();
			},
			
			/* 改变page[当前页码]调用方法*/
			handleCurrentChange(page) {
				/* 只要改变马上调用分页查询 */
				this.SelectProductLevelByNumName();
			},
			
			/* 定时器 */
			currentTime() { //定时器
				setInterval(this.getDate, 100)
			},
			/* 获取当前时间 */
			getDate() {
				const year = new Date().getFullYear()
				const month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1)
				const date = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
				const hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
				const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
				const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
				const week = new Date().getDay()
				this.nowTime = year + '年' + month + '月' + date + '日' + ' ' + hh + ':' + mm + ':' + ss
			},
			/* 获取当前用户选择的行 */
			handleSelectionChang(value) {
				this.selections = value;
			},
		},
		// 销毁定时器
		beforeDestroy: function() {
			if (this.getDate) {
				clearInterval(this.getDate) // 在Vue实例销毁前，清除时间定时器
			}
		},
	}
</script>

<style scoped>
	.el-header {
		background-color: #333333;
	}
</style>
